<?php
require_once("Config.php");
require_once("Util.php");
require_once("PicInfo.php");
require_once("MsgStack.php");
session_start();
$msg = new MsgStack();
if ($_GET['msg'] != '') {
    $message = $_SESSION[$_GET['msg']];
}

$sql = "select * from gallery where id=" . $_GET['id'];
$query = mysql_query($sql);
$pic = new PicInfo();
if ($query) {
    while ($res = mysql_fetch_array($query)) {
        $pic->setId($res["id"]);
        $pic->setOrder($res["order"]);
        $pic->setTitle($res["title"]);
        $pic->setNote($res["note"]);
        $pic->setSmallPic($res["smallPic"]);
        $pic->setLargePic($res["largePic"]);
        $pic->setAddtime($res["addtime"]);
    }
} else {
    $msg->push("Pic not found!Cannot edit inexistent picture.");
    $_SESSION["GALLERY_ERR_EDIT_PIC_NOT_FOUND"] = $msg->getMsg();
    Util::redirect("gallery.php", "GALLERY_ERR_EDIT_PIC_NOT_FOUND");
    exit();
}
?>
<html>
    <head>
        <title>Add Pics !</title>
        <link href="css/admin-common.css" rel="stylesheet" type="text/css">
        <link type="text/css" href="css/ui-lightness/jquery-ui-1.7.3.custom.css" rel="stylesheet" />
        <script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
        <script type="text/javascript" src="js/jquery-ui-1.7.3.custom.min.js"></script>
        <script type="text/javascript" src="ckeditor/ckeditor.js"></script>
        <script type="text/javascript" src="js/common.js">
        </script>
    </head>
    <body>
        <p> <?php echo $message; ?> </p>
        <input type="hidden" name='messageFlag' id='messageFlag' value='<?php echo $messageFlag ?>'/>
        <h3>Gallery Management : Update Picture</h3>
        <hr/>
        <p>
            <label> <Strong><a href="gallery.php">&lt; Back to Pics LIST Page</a></Strong> </label>
        </p>

        <!--Add User Form-->
        <form action="gallery_update.php" enctype="multipart/form-data" method="post" name="addNewsForm" id="addNewsForm">
        <input type="hidden" id="id" name="id" value="<?php echo $pic->getId();?>"/>
            <label> <span>*Pic title</span>
                <input type="text" id="title" name="title" style="width:500px;" value="<?php echo $pic->getTitle(); ?>"/>
            </label>
            <p> <a href="<?php echo $pic->getlargePic(); ?>"><img alt=""  src="<?php echo $pic->getSmallPic(); ?>"/></a> </p>
            
            <div>
            <input type="hidden" id="ifChangePic" name="ifChangePic" value="no"/>
            	Show file picker for updating picture:<input type="checkbox" id="cbox" value="cbox" onclick="app.showHideImgPicker()" /> 
            </div>
            <div id="imgForm">
                <label> <span>*Small pic(thumbnail):</span>
                    <input type="file" name="smallPic" id="smallPic" />
                </label>
                <label> <span>*Large pic(formal):</span>
                    <input type="file" name="largePic" id="largePic" />
                </label>
            </div>
            <label> <span>Pic note:</span> <br/>
                <textarea cols="50" id="note" name="note" rows="5" style="font-size:12px;"><?php echo $pic->getNote(); ?></textarea>
            </label>
            <p>
                <label> <span>&nbsp;</span>
                    <button class="btn btn-style-1" type="button" onClick="return app.validInput()" ><b><b>Save</b></b></button>
                </label>
            </p>
        </form>
        <div id='validInputFailureDlg' title='Incomplete news info!'> <em><strong>Pic title</strong></em>,<em><strong>Small Pic</strong></em> and <em><strong>Large Pic</strong></em> are required,Please fill them all! </div>
        <div id='addNewsOkDlg' title="Confirmation" style='display:none'> You have just posted a news story,would you like to post more? </div>
    </body>
    <script type="text/javascript">
        var app={
            init: function() {
				$("#imgForm").hide();
                $('#validInputFailureDlg').dialog({
                    bgiframe: true,
                    modal: true,
                    draggable:false,
                    autoOpen:false,
                    width:300,

                    buttons: {
                        Ok: function() {
                            $(this).dialog('close');
                        }

                    }
                });
				
                $('#addNewsOkDlg').dialog({
                    bgiframe: true,
                    modal: true,
                    draggable:false,
                    autoOpen:false,
                    width:300,

                    buttons: {
                        'Yes,I want to': function() {
                            document.location.replace('news_addForm.php');
                            $(this).dialog('close');
                        },

                        'No,go back to news list': function() {
                            document.location.replace('news.php');
                        }

                    }
                });

                if($('#messageFlag').val()=='1') {

                    $('#addNewsOkDlg').dialog('open');
                }
            },

            validInput: function() {
                var contentObj =CKEDITOR.instances.content;
				if($("#cbox").attr("checked")){
					$('#validInputFailureDlg').html(" <em><strong>Pic title</strong></em>,<em><strong>Small Pic</strong></em> and <em><strong>Large Pic</strong></em> are required,Please fill them all! ");
					if(jQuery.trim($('#title').val())=="" || jQuery.trim($('#smallPic').val())=="" || jQuery.trim($('#largePic').val())=="") {

                    $('#validInputFailureDlg').dialog('open');

                    return false;
                }
                $('#addNewsForm').submit();
                return true;
					
				}else{
					$('#validInputFailureDlg').html('<em><strong>Pic title</strong></em> cannot beempty!');
					if(jQuery.trim($('#title').val())=="") {

                    $('#validInputFailureDlg').dialog('open');

                    return false;
                }
                $('#addNewsForm').submit();
                return true;
					
				}

                

            },
			showHideImgPicker:function(){
				
				if($("#cbox").attr("checked")){
				
					$("#ifChangePic").val("yes");
					$("#imgForm").show();
				}else{
				$("#ifChangePic").val("no");
					$("#imgForm").hide();
				}	
		    }

        }
    </script>
</html>
